package com.venus.controller;

import com.venus.entity.Massage;
import com.venus.entity.Register;
import com.venus.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static com.venus.common.Commons.SESSION_USER_KEY;

/**
 * Created by lanou on 18/3/1.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseController.class);
    @Resource
    private UserService userService;

    @RequestMapping("/getUserInfo")
    public ModelAndView getUserInfo(ModelAndView mav, HttpSession session){
       String username =(String) session.getAttribute(SESSION_USER_KEY);
        Register register = new Register();
        register.setUsername(username);
       int uid = userService.findUid(register);
       Massage massage = userService.findMassage(uid);
       mav.addObject("massage",massage);
       mav.setViewName("base/massage");
        return mav;
    }
    @RequestMapping("/massageForm")
    @ResponseBody
    public Map<String, String> massageForm(@RequestParam() Map<String, Object> params,Massage massage,HttpSession session){
        Map<String, String> result = new HashMap<>();
        String username =(String) session.getAttribute(SESSION_USER_KEY);
        Register register = new Register();
        register.setUsername(username);
        int uid = userService.findUid(register);
        userService.upMassage(uid,massage);

        LOGGER.info(params.toString());
        result.put("result", "success");
        return result;
    }
    @RequestMapping("newpwd")
    public String newpwd(){
        return "base/code";
    }
    @RequestMapping("pwdForm")
    @ResponseBody
    public Map<String, String> pwdFrom(@RequestParam() Map<String, Object> params,String oldpwd,String newpwd,HttpSession session){
        Map<String, String> result = new HashMap<>();
        String username =(String) session.getAttribute(SESSION_USER_KEY);
        if(userService.valiPwd(username,oldpwd)==true){
            userService.upPwd(username,newpwd);
            LOGGER.info(params.toString());
            result.put("result", "success");
        }else {
        result.put("result", "error");
        }
        return result;
    }
    @RequestMapping("/upImg")
    @ResponseBody
    public Map<String,String> upImg(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        Map<String,String> map = new HashMap<>();
        if(file.isEmpty()){
            map.put("code","500");
            map.put("msg","上传失败");
            map.put("data","src:null");
            return map;
        }
        String fileName1 = file.getOriginalFilename();
        String[] a=fileName1.split("\\.");
        String uuid = UUID.randomUUID().toString();
        String fileName = uuid+"."+a[1];
        String filePath = "/Users/lanou/IdeaProjects2/base-handler/src/main/resources/resources/avatar/";
        File dest = new File(filePath + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            String username =(String) request.getSession().getAttribute(SESSION_USER_KEY);
            Register register = new Register();
            register.setUsername(username);
            int uid = userService.findUid(register);
            String avatar = "http://localhost:8080/avatar/"+fileName;
            userService.upImg(uid,avatar);
            map.put("code","200");
            map.put("msg","上传成功");
            map.put("data","src:avatar/"+dest.getName());
            return map;

        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("code","500");
        map.put("msg","上传失败");
        map.put("data","src:null");
        return map;
    }



}
